package com.alogic.xscript.plugins;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import com.alogic.xscript.AbstractLogiclet;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;

/**
 * 字符串数组组合
 * @author duanwaiwai
 * @since 1.6.12.20 [20190130 duanyy]
 */
public class StringListGroup extends Segment {

	protected static final String DEFAULT_GROUP = "group";
	
	protected String cid = "$stringlistgroup";
	
	public StringListGroup(String tag, Logiclet p) {
		super(tag, p);
		
		registerModule("strlist-group-add",Add.class);
		registerModule("strlist-group-scan",Scan.class);
		registerModule("strlist-group-get",Get.class);
	}

	@Override
	public void configure(Properties p){
		super.configure(p);
		cid = PropertiesConstants.getString(p,"cid",cid,true);
	}

	@Override
	protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
		Map<String,List<String>> map = new HashMap<String,List<String>>();
		
		try {
			ctx.setObject(cid, map);				
			super.onExecute(root, current, ctx, watcher);
		}finally{
			ctx.removeObject(cid);				
		}
	}	
	
	/**
	 * 向数组中增加值
	 * 
	 * @author duanyy
	 *
	 */
	public static class Add extends AbstractLogiclet {
		protected String pid = "$stringlistgroup";
		protected String $group = DEFAULT_GROUP;
		protected String $value = "";
		protected boolean raw = false;
		protected boolean ref = false;
		
		public Add(String tag, Logiclet p) {
			super(tag, p);
		}

		@Override
		public void configure(Properties p){
			super.configure(p);
			pid = PropertiesConstants.getString(p,"pid",pid,true);
			$value = PropertiesConstants.getRaw(p,"value",$value);
			$group = PropertiesConstants.getRaw(p,"group",$group);
			raw = PropertiesConstants.getBoolean(p, "raw", raw,true);
			ref = PropertiesConstants.getBoolean(p, "ref", ref,true);
		}
		
		protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {	
			Map<String,List<String>> map = ctx.getObject(pid);
			if (map != null){
				String group = PropertiesConstants.transform(ctx, $group, DEFAULT_GROUP);
				String value = null;
				if (raw){
					value = ref?PropertiesConstants.getRaw(ctx, $value, ""):$value;
				}else{
					value = ref?PropertiesConstants.getString(ctx,$value,"",false):PropertiesConstants.transform(ctx, $value, "");
				}
				if (StringUtils.isNotEmpty(value)){
					List<String> found = map.get(group);
					if (found == null){
						synchronized (map){
							found = map.get(group);
							if (found == null){
								found = new ArrayList<String>();
								map.put(group, found);
							}
						}
					}
					found.add(value);
				}
			}
		}	
	}	
	
	/**
	 * 遍历输出
	 * @author duanwaiwai
	 *
	 */
	public static class Scan extends Segment {
		protected String pid = "$stringlistgroup";
		protected String delimeter = ",";
		protected String groupId = "$key";
		protected String valueId = "$value";
		
		
		public Scan(String tag, Logiclet p) {
			super(tag, p);
		}

		@Override
		public void configure(Properties p){
			super.configure(p);
			pid = PropertiesConstants.getString(p,"pid",pid,true);
			delimeter = PropertiesConstants.getString(p,"delimeter",delimeter,true);
			groupId = PropertiesConstants.getString(p,"groupId",groupId,true);
			valueId = PropertiesConstants.getString(p,"valueId",valueId,true);
		}
		
		protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
			Map<String,List<String>> map = ctx.getObject(pid);
			if (map != null){
				Iterator<Entry<String,List<String>>> iter = map.entrySet().iterator();
				while (iter.hasNext()){
					Entry<String,List<String>> entry = iter.next();					
					try {
						ctx.SetValue(groupId, entry.getKey());
						ctx.SetValue(valueId, StringUtils.join(entry.getValue().toArray(),delimeter));
						super.onExecute(root, current, ctx, watcher);
					}finally{
						ctx.SetValue(groupId, "");
						ctx.SetValue(valueId, "");
					}
				}
			}
		}	
	}
	
	/**
	 * 获取指定group输出
	 * @author duanyy
	 *
	 */
	public static class Get extends Segment {
		protected String pid = "$stringlistgroup";
		protected String delimeter = ",";
		protected String $id = "";
		protected String $group = DEFAULT_GROUP;
		
		public Get(String tag, Logiclet p) {
			super(tag, p);
		}

		@Override
		public void configure(Properties p){
			super.configure(p);
			pid = PropertiesConstants.getString(p,"pid",pid,true);
			delimeter = PropertiesConstants.getString(p,"delimeter",delimeter,true);
			$id = PropertiesConstants.getRaw(p,"id",$id);
			$group = PropertiesConstants.getRaw(p,"group",$group);
		}
		
		protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
			String id = PropertiesConstants.transform(ctx, $id, "");
			if (StringUtils.isNotEmpty(id)){			
				String group = PropertiesConstants.transform(ctx,$group,DEFAULT_GROUP);
				Map<String,List<String>> map = ctx.getObject(pid);
				if (map != null){
					List<String> list = map.get(group);
					if (list != null){
						ctx.SetValue(id, StringUtils.join(list.toArray(new String[list.size()]), delimeter));
					}
				}
			}
		}	
	}	
}
